import scrapy


class FanyiSpiderSpider(scrapy.Spider):
    name = 'fanyi_spider'
    # allowed_domains = ['www.baidu.com']
    start_urls = ['https://fanyi.so.com/index/search?eng=1&validate=&ignore_trans=0&query=hello%0A']

    def start_requests(self):
        data = {
            'eng': '1',
            'query': 'hello'
        }
        yield scrapy.FormRequest(url=self.start_urls[0], callback=self.parse, formdata=data)

    def parse(self, response,**kwargs):
        question = response.json()
        print(question['data']['fanyi'])
        trs = input("请输入要翻译的内容:")
        data = {
            'eng': '1',
            'query': trs
        }
        panduan = trs[0].encode('utf-8')
        if len(panduan) == 3:
            data['eng'] = '0'
        else:
            data['eng'] = '1'

        yield scrapy.FormRequest(url=self.start_urls[0], callback=self.parse, formdata=data, dont_filter=True)
